﻿using System;
using System.IO;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Windows.Storage;
using Windows.Storage.Streams;

namespace Tools
{
    public sealed class Win8StorageHelper
    {
        private static StorageFolder defaultFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

        public async static Task<bool> SaveData(string path, object data)
        {
            return await SaveData(path, defaultFolder, data);
        }

        public async static Task<bool> SaveData(string path, StorageFolder folder, object data)
        {
            // settings
            var option = Windows.Storage.CreationCollisionOption.ReplaceExisting;

            try
            {
                var file = await folder.CreateFileAsync(path, option);
                MemoryStream saveData = new MemoryStream();

                XmlSerializer x = new XmlSerializer(data.GetType());
                x.Serialize(saveData, data);

                if (saveData.Length > 0)
                {

                    // Get an output stream for the SessionState file and write the state asynchronously
                    using (var fileStream = await file.OpenStreamForWriteAsync())
                    {
                        saveData.Seek(0, SeekOrigin.Begin);
                        await saveData.CopyToAsync(fileStream);
                        await fileStream.FlushAsync();
                    }
                    return true;
                }
            }
            catch { }

            return false;
        }

        public async static Task<bool> SaveData(string path, string data)
        {
            return await SaveData(path, defaultFolder, data);
        }

        public async static Task<bool> SaveData(string path, StorageFolder folder, string data)
        {
            // settings
            var option = Windows.Storage.CreationCollisionOption.ReplaceExisting;

            try
            {
                var file = await folder.CreateFileAsync(path, option);
                using (StreamWriter writer = new StreamWriter(await file.OpenStreamForWriteAsync()))
                {
                    await writer.WriteLineAsync(data);
                }
                return true;
            }
            catch { }

            return false;

        }

        public async static Task<object> LoadData(string path, System.Type type)
        {
            return await LoadData(path, defaultFolder, type);
        }

        public async static Task<object> LoadData(string path, StorageFolder folder, System.Type type)
        {
            try
            {
                var file = await folder.GetFileAsync(path);

                using (IInputStream inStream = await file.OpenSequentialReadAsync())
                {
                    // Deserialize the Session State
                    XmlSerializer x = new XmlSerializer(type);
                    return x.Deserialize(inStream.AsStreamForRead());
                }
            }
            catch { return null; }
        }

        public async static Task<string> LoadData(string path)
        {
            return await LoadData(path, defaultFolder);
        }

        public async static Task<string> LoadData(string path, StorageFolder folder)
        {
            try
            {
                var file = await folder.GetFileAsync(path);
                using (StreamReader sr = new StreamReader(await file.OpenStreamForReadAsync()))
                {
                    return sr.ReadToEnd();
                }
            }
            catch { return null; }
        }

        public static void SafeDeleteFile(string filename)
        {
            SafeDeleteFile(defaultFolder, filename);
        }

        public static void SafeDeleteFile(StorageFolder folder, string filename)
        {
            try
            {
                DeleteFile(folder, filename);
            }
            catch { }
        }

        public static void DeleteFile(string filename)
        {
            DeleteFile(defaultFolder, filename);
        }

        public async static void DeleteFile(StorageFolder folder, string filename)
        {
            var file = await folder.GetFileAsync(filename);
            await file.DeleteAsync();
        }

        public async static Task<bool> CheckifFileExists(string filename)
        {
            return await CheckifFileExists(defaultFolder, filename);
        }

        public async static Task<bool> CheckifFileExists(StorageFolder folder, string filename)
        {
            try
            {
                var file = await folder.GetFileAsync(filename);
                return file == null ? false : true;
                {

                }
            }
            catch
            {
                return false;
            }
        }
        
    }
}